package com.yuyuan.blog.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.yuyuan.blog.domain.po.BlogAgreement;
import com.yuyuan.blog.domain.po.BlogDaily;
import com.yuyuan.blog.domain.po.BlogDeal;
import com.yuyuan.blog.domain.po.BlogPractice;
import com.yuyuan.blog.mapper.ElasticSearch.AgreementBlogRepository;
import com.yuyuan.blog.mapper.ElasticSearch.DailyBlogRepository;
import com.yuyuan.blog.mapper.ElasticSearch.DealBlogRepository;
import com.yuyuan.blog.mapper.ElasticSearch.PracticeBlogRepository;
import com.yuyuan.blog.service.IElasticSearch;
import com.yuyuan.common.domain.Result;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

@Service
public class ElasticSearchServiceImpl implements IElasticSearch {

    @Autowired
    private AgreementBlogRepository agreementBlogRepository;

    @Autowired
    private DealBlogRepository dealBlogRepository;

    @Autowired
    private PracticeBlogRepository practiceBlogRepository;

    @Autowired
    private DailyBlogRepository dailyBlogRepository;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public void saveAgreementBlog(BlogAgreement blogAgreement) {
        agreementBlogRepository.save(blogAgreement);
    }
    public void saveDealBlog(BlogDeal blogDeal) {
        dealBlogRepository.save(blogDeal);
    }
    public void savePracticeBlog(BlogPractice blogPractice) {
        practiceBlogRepository.save(blogPractice);
    }
    public void saveDailyBlog(BlogDaily blogDaily) {
        dailyBlogRepository.save(blogDaily);
    }
    public void deleteAgreementBlog(Long id) {
        agreementBlogRepository.deleteById(id);
    }
    public void deleteDealBlog(Long id) {
        dealBlogRepository.deleteById(id);
    }
    public void deletePracticeBlog(Long id) {
        practiceBlogRepository.deleteById(id);
    }
    public void deleteDailyBlog(Long id) {
        dailyBlogRepository.deleteById(id);
    }

    public Result searchAgreementBlog(String keyword, Short blogType) throws IOException {
        SearchRequest searchRequest = new SearchRequest("agreement");
        // 构建bool查询
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("agreementType", blogType))
                .should(QueryBuilders.wildcardQuery("name", keyword))
                .should(QueryBuilders.wildcardQuery("content", keyword))
                .minimumShouldMatch(1); // 至少匹配一个should子句
        // 构造搜索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
                .query(boolQuery)
                .sort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
                .from(0);// 指定从哪条开始查询

        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        List<BlogAgreement> list = new LinkedList<>();
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            BlogAgreement blogAgreement = JSONObject.parseObject(hit.getSourceAsString(), BlogAgreement.class);
            list.add(blogAgreement);
        }
        System.out.println(list.size());
        for (BlogAgreement blogAgreement : list) {
            blogAgreement.handlerImgs();
            System.out.println(blogAgreement);
        }
        return Result.ok(list);
    }

    @Override
    public Result searchDealBlog(String keyword, Short type) throws IOException {
        SearchRequest searchRequest = new SearchRequest("deal");
        // 构建bool查询
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("type", type))
                .should(QueryBuilders.wildcardQuery("name", keyword))
                .should(QueryBuilders.wildcardQuery("content", keyword))
                .minimumShouldMatch(1); // 至少匹配一个should子句
        // 构造搜索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
                .query(boolQuery)
                .sort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
                .from(0);// 指定从哪条开始查询

        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        List<BlogDeal> list = new LinkedList<>();
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            BlogDeal blogDeal = JSONObject.parseObject(hit.getSourceAsString(), BlogDeal.class);
            list.add(blogDeal);
        }
        System.out.println(list.size());
        for (BlogDeal blogDeal : list) {
            blogDeal.handlerImgs();
            System.out.println(blogDeal);
        }
        return Result.ok(list);
    }

    @Override
    public Result searchPracticeBlog(String keyword) throws IOException {
        SearchRequest searchRequest = new SearchRequest("practice");
        // 构建bool查询
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .should(QueryBuilders.wildcardQuery("demand", keyword))
                .should(QueryBuilders.wildcardQuery("content", keyword))
                .should(QueryBuilders.wildcardQuery("location", keyword))
                .minimumShouldMatch(1); // 至少匹配一个should子句
        // 构造搜索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
                .query(boolQuery)
                .sort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
                .from(0);// 指定从哪条开始查询

        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        List<BlogPractice> list = new LinkedList<>();
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            BlogPractice blogPractice = JSONObject.parseObject(hit.getSourceAsString(), BlogPractice.class);
            list.add(blogPractice);
        }
        System.out.println(list.size());
        for (BlogPractice blogPractice : list) {
            blogPractice.handlerImgs();
            System.out.println(blogPractice);
        }
        return Result.ok(list);
    }

    @Override
    public Result searchDailyBlog(String keyword, Short type) throws IOException {
        SearchRequest searchRequest = new SearchRequest("daily");
        // 构建bool查询
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("type", type))
                .should(QueryBuilders.wildcardQuery("content", keyword));
        // 构造搜索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
                .query(boolQuery)
                .sort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
                .from(0);// 指定从哪条开始查询

        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        List<BlogDaily> list = new LinkedList<>();
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            BlogDaily blogDaily = JSONObject.parseObject(hit.getSourceAsString(), BlogDaily.class);
            list.add(blogDaily);
        }
        System.out.println(list.size());
        for (BlogDaily blogDaily : list) {
            blogDaily.handlerImgs();
            System.out.println(blogDaily);
        }
        return Result.ok(list);
    }

}
